﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml.Serialization;

namespace ExcelExportLib
{
    [XmlRoot("Font")]
    public class ExcelCellStyleFont : IXmlSerializable
    {
        /// <summary>Text color in cell</summary>
        public Color? Color;
        /// <summary>Bold text in cell</summary>
        public bool? Bold;
        /// <summary>Underline text in cell</summary>
        public eFontUnderline? Underline;
        /// <summary>Italic text in cell</summary>
        public bool? Italic;
        /// <summary>Font family</summary>
        public eFontFamily? FontFamily;

        public string Name;
        public int? Charset;

        private Double? _size;
        /// <summary>Font Size</summary>
        public Double? Size
        {
            get { return _size; }
            set 
            {
                if (value <= 0)
                {
                    _size = null;
                    throw new ArgumentException("Font size must be greater than 0", "Size");
                }
                else
                    _size = value;
            }
        }

        public ExcelCellStyleFont()
        {
        }

        public ExcelCellStyleFont(System.Drawing.Font font)
        {
            if(font==null)
                throw new NullReferenceException("Font must be not null!");

            Name = font.Name;
            if(font.Bold)
                Bold = true;
            if(font.Underline)
                Underline = eFontUnderline.SingleAccounting;
            if(font.Italic)
                Italic = true;
            Size = font.Size;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteExcelAttribute("FontName", Name);
            writer.WriteExcelAttribute("Charset", Charset);
            writer.WriteExcelAttribute("Bold", Bold);
            writer.WriteExcelAttribute("Italic", Italic);
            writer.WriteExcelAttribute("Underline", Underline);
            writer.WriteExcelAttribute("Color", Color);
            writer.WriteExcelAttribute("Size", Size);
        }

        #endregion
    }
}
